package com.contactbook.web.controller.account;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.contactbook.web.service.AccountClient;
import com.contactbook.web.service.ContactClient;
import com.contactbook.web.service.GroupClient;
import com.contactbook.web.util.AccountMessage;

import contactbook.model.Account;

public class UpdateAccountController extends HttpServlet implements Servlet {

	private static final long serialVersionUID = 1L;
	
	private static final String CONFIRM_UPDATE = "OK";
	private static final String CANCEL_UPDATE = "Annuler";
	
	private AccountClient accountClient;
	private GroupClient groupClient;
	private ContactClient contactClient;
	
	public UpdateAccountController() {
		super();
    	accountClient = new AccountClient();
		groupClient = new GroupClient();
		contactClient = new ContactClient();
	}
	
	public void init() throws ServletException {
    	super.init();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher dispatcher = request.getRequestDispatcher("/Index");
		dispatcher.forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		Account account = (Account)session.getAttribute("account");
		String submit = request.getParameter("submit");
		RequestDispatcher dispatcher = request.getRequestDispatcher("/Index");
		
		if (account != null) { 
			groupClient.login(account.getLogin(), account.getPassword());
			contactClient.login(account.getLogin(), account.getPassword());
		
			if (submit == null) {
				dispatcher = request.getRequestDispatcher("/updateAccount.jsp");
			}
			else if (submit.equals(CONFIRM_UPDATE)) {
				AccountMessage message = updateAccount(request);
				request.setAttribute("message", message);
				request.setAttribute("login", request.getParameter("login"));
				dispatcher = request.getRequestDispatcher("/updateAccount.jsp");
			}
			else if (submit.equals(CANCEL_UPDATE)) {
				dispatcher = request.getRequestDispatcher("/Account");
			}
			
			groupClient.logout();
			contactClient.logout();
		}

		dispatcher.forward(request, response);
	}
	
	private AccountMessage updateAccount(HttpServletRequest request) {
		HttpSession session = request.getSession(true);
		Account account = (Account)session.getAttribute("account");
		
		if (account != null) { 
			String login = request.getParameter("login");
			String password = request.getParameter("password");
			String confirmPassword = request.getParameter("confirmPassword");
			
			if (!login.equals("") && !password.equals("")) {
				if (password.equals(confirmPassword)) {
					accountClient.login(account.getLogin(), account.getPassword());
					account.setLogin(login);
					account.setPassword(password);
					accountClient.update(account);
					session.setAttribute("account", account);
					accountClient.logout();
			        return AccountMessage.ACCOUNT_UPDATED_OK;
				}
				else
					return AccountMessage.ACCOUNT_UPDATED_PWD_CONFIRM_FAIL;
			}
			return AccountMessage.ACCOUNT_UPDATED_FORM_FAIL;
		}
		return AccountMessage.ACCOUNT_UPDATED_FAIL;
	}
	
	public void destroy() {
		super.destroy();
	}
	
}
